package favorite

import (
	"context"
	"strings"

	"mall/common/globalkey"
	"mall/common/jwt"
	"mall/common/resultx"
	"mall/common/utils/pointy"
	"mall/service/forum/api/internal/svc"
	"mall/service/forum/api/internal/types"
	"mall/service/forum/rpc/forumclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteShopGoodsFavoriteLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDeleteShopGoodsFavoriteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteShopGoodsFavoriteLogic {
	return &DeleteShopGoodsFavoriteLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DeleteShopGoodsFavoriteLogic) DeleteShopGoodsFavorite(
	req *types.DeleteShopGoodsFavoriteReq,
	headers *[]*types.Header,
) (resp *types.BaseMsgResp, err error) {
	// if (req.Ids == nil || len(strings.TrimSpace(*req.Ids)) == 0) && (req.ProductIds == nil || len(strings.TrimSpace(*req.ProductIds)) == 0) && req.Clear == nil {
	if (req.Ids == nil || len(strings.TrimSpace(*req.Ids)) == 0) && (req.ProductIds == nil || len(strings.TrimSpace(*req.ProductIds)) == 0) && req.Clear == nil {
		return nil, resultx.NewErrCode(resultx.PRODUCT_ID_REQUIRED)
	}

	userId := jwt.GetUserId(l.ctx)
	if userId == 0 {
		return nil, resultx.NewErrCode(resultx.TOKEN_EXPIRE_ERROR)
	}

	_, err = l.svcCtx.ForumRpc.DeleteUserCollect(l.ctx, &forumclient.RemoveUserCollectReq{
		Ids:        req.Ids,
		ObjectIds:  req.ProductIds,
		ObjectType: pointy.GetPointer(uint32(globalkey.AttachmentLogTypeProduct)),
		VisitorId:  pointy.GetPointer(userId),
		Clear:      req.Clear,
	})
	if err != nil {
		return nil, err
	}

	return &types.BaseMsgResp{}, nil
}
